package com.weayer.remote;


import com.weayer.constant.AmapConfigConstants;
import com.weayer.constant.CommonStatusEnum;
import com.weayer.dto.ResponseResult;
import com.weayer.response.DirectionResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @description: 调用地图服务Client
 * @author: Weayer
 **/
@Service
public class MapDirectionClient {

    @Value("${amap.key}")
    private String amapKey;
    @Autowired
    private RestTemplate restTemplate;

    public DirectionResponse direction(String depLongitude, String depLatitude, String desLongitude, String desLatitude) {
        // 1. 组装url: https://restapi.amap.com/v3/direction/driving?origin=110.78,32.65&destination=110.78,32.60&output=json&strategy=11&key=d162421af934edc67f8eb0450ec183e5

        //      https://restapi.amap.com/v3/direction/driving
        //      ?origin=110.78,32.65
        //      &destination=110.78,32.60
        //      &output=json
        //      &strategy=11
        //      &key=d162421af934edc67f8eb0450ec183e5

        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(AmapConfigConstants.DIRECTION_URL_PREFIX);
        urlBuilder.append("?")
                .append("origin=" + depLongitude + "," + depLatitude)
                .append("&")
                .append("destination=" + desLongitude + "," + desLatitude)
                .append("&")
                .append("output=json")
                .append("&")
                .append("strategy=11")
                .append("&")
                .append("key=" + amapKey);
        System.out.println(urlBuilder.toString());

        // 2. 调用高德地图api
        ResponseEntity<String> directionEntity = restTemplate.getForEntity(urlBuilder.toString(), String.class);

        // 3. 解析接口
        DirectionResponse directionResponse = parseDirectionEntity(directionEntity.getBody());

        return directionResponse;
    }
    private DirectionResponse parseDirectionEntity(String directionString){
        DirectionResponse directionResponse = null;
        try {
            JSONObject result = JSONObject.fromObject(directionString);
            if(result.has(AmapConfigConstants.STATUS)){
                int status = result.getInt(AmapConfigConstants.STATUS);
                if(status == 1){
                    if(result.has(AmapConfigConstants.ROUTE)){
                        JSONObject routeObject = result.getJSONObject(AmapConfigConstants.ROUTE);
                        JSONArray pathsArray = routeObject.getJSONArray(AmapConfigConstants.PATHS);
                        JSONObject pathObject = pathsArray.getJSONObject(0);
                        directionResponse = new DirectionResponse();
                        if(pathObject.has(AmapConfigConstants.DISTANCE)){
                            int distance = pathObject.getInt(AmapConfigConstants.DISTANCE);
                            directionResponse.setDistance(distance);
                        }
                        if(pathObject.has(AmapConfigConstants.DURATION)){
                            int duration = pathObject.getInt(AmapConfigConstants.DURATION);
                            directionResponse.setDuration(duration);
                        }
                    }
                }else{
                    return null;
                }
            }
        }catch (Exception e){

        }
        return directionResponse;
    }
}
